package cc.mrbird.febs.pay.ali;

import cc.mrbird.febs.common.entity.PayConstant;
import cc.mrbird.febs.common.utils.OrderNoUtil;
import cc.mrbird.febs.pay.base.AbstractPayStrategy;
import cc.mrbird.febs.pay.base.PayModelFactory;
import cc.mrbird.febs.pay.enums.ALiTradeState;
import cc.mrbird.febs.pay.enums.PayType;
import cc.mrbird.febs.payment.entity.PaymentInfo;
import cc.mrbird.febs.payment.service.IPaymentInfoService;
import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.kernel.util.ResponseChecker;
import com.alipay.easysdk.payment.page.models.AlipayTradePagePayResponse;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;
import java.util.Objects;


/**
 * @ClassName AppPay
 * @Description 支付宝 app支付
 * @Author 狗蛋儿
 * @Date 2020/11/26 11:15
 * @Version V1.0
 **/
@Slf4j
@Service
@AllArgsConstructor
public class ALiPayPCPay extends AbstractPayStrategy {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private IPaymentInfoService iPaymentInfoService;

    @Override
    public synchronized Object pay(Map<String, String> parameterMap) throws Exception {
        String subject = parameterMap.get("subject");
        String orderNo = OrderNoUtil.getOrderNum(2656L);
        System.out.println(orderNo);
        String totalAmount = parameterMap.get("totalAmount");
        PaymentInfo paymentInfo = iPaymentInfoService.getPaymentInfoByOrderSn(orderNo);
        if (Objects.nonNull(paymentInfo) && StringUtils.isNotBlank(paymentInfo.getContent())) {
            return paymentInfo.getContent();
        }
        AlipayTradePagePayResponse response = Factory.Payment.Page().asyncNotify(
                stringRedisTemplate.opsForValue().get(PayConstant.SUFFIX + PayConstant.PAY_ALI_PAY_NOTIFY_URL)).pay(subject, orderNo, totalAmount,
                stringRedisTemplate.opsForValue().get(PayConstant.SUFFIX + PayConstant.PAY_ALI_PAY_RETURN_URL)
        );
        if (ResponseChecker.success(response)) {
            //插入支付记录
            iPaymentInfoService.createPaymentInfo(new PaymentInfo()
                    .setPaymentType(PayType.A_LI_PAY_APP.getLabel())
                    .setUpdateTime(new Date())
                    .setCreateTime(new Date())
                    .setOrderNo(orderNo)
                    .setContent(response.getBody())
                    .setPayerTotal(new BigDecimal(totalAmount).multiply(BigDecimal.valueOf(100)).intValue())
                    .setTradeType(PayType.A_LI_PAY_APP.getTradeType())
                    .setTransactionId(iPaymentInfoService.getTransactionId(orderNo))
                    .setTradeState(ALiTradeState.WAIT_BUYER_PAY.getType()));
            return response.getBody();
        }
        throw new RuntimeException("统一下单失败");
    }

    @Override
    public Object cancel(Map<String, String> parameterMap) throws Exception {
        return null;
    }


    /**
     * 根据订单查询支付记录
     *
     * @param parameterMap@return
     * @throws
     */
    @Override
    public Object queryPay(Map<String, String> parameterMap) throws RuntimeException {
        return null;
    }

    /**
     * 退款
     *
     * @param parameterMap
     * @return
     * @throws
     */
    @Override
    public Object refund(Map<String, String> parameterMap) throws RuntimeException {
        return null;
    }

    /**
     * 退款查询
     *
     * @param stringStringMap
     * @return
     * @throws
     */
    @Override
    public Object queryRefund(Map<String, String> stringStringMap) throws RuntimeException {
        return null;
    }


    @Override
    public void afterPropertiesSet() throws Exception {
        PayModelFactory.register(PayType.A_LI_PAY_PC.getCode(), this);
    }
}
